package br.com.midiaseducativas.tutor.jogos.quiz {
	import br.com.midiaseducativas.tutor.events.FeedBackEvent;
	import br.com.midiaseducativas.tutor.jogos.quiz.vo.AnswerVO;
	import br.com.midiaseducativas.tutor.jogos.quiz.vo.QuestionVO;
	
	import com.demonsters.debugger.MonsterDebugger;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	
	public class GameCore {
		
		private var quiz:QuizStage;
		
		private var xmlLoader:URLLoader;
		
		private var quizStage:QuizStage;
		
		private var quizXML:XML;
		
		private var questions:Array;
		
		private var quizCount:uint;
		
		private var timer:Timer;
		
		private var time:Number;
		
		public function GameCore(pQuiz:QuizStage, pXMLString:String) {
			try {
				this.xmlLoader = new URLLoader();
				this.xmlLoader.addEventListener(Event.COMPLETE, onLoaderComplete);
				//MonsterDebugger.trace(this, "Passou 1 - "+ pXMLString);
				this.xmlLoader.load(new URLRequest(pXMLString));
				//MonsterDebugger.trace(this, "Passou 2");
				quizStage = pQuiz;
			} catch (e:Error){
				//MonsterDebugger.trace(this, e);
			}
			this.timer = new Timer(1);
			this.time = 0;
			this.timer.addEventListener(TimerEvent.TIMER, onTimerTick);
			this.timer.start();
			
			this.quizStage.ok.visible = false;
			this.quizCount = 0;
		}
		
		private function onLoaderComplete(pEvent:Event):void {
			
			this.quizXML = new XML(this.xmlLoader.data);
			this.quizStage.begin.addEventListener(MouseEvent.CLICK, onBeginClick);
			this.quizStage.begin.buttonMode = true;
			//MonsterDebugger.trace(this, this.quizXML);
			this.quizStage.title.text = this.quizXML..title;
			this.getVOs();
			
			
			
			
		}
		
		private function getVOs():void
		{
			this.questions = new Array;
			var quest:QuestionVO;
			var answer:AnswerVO;
			var question:Question;
			for (var index:String in this.quizXML..quest) {
				quest = new QuestionVO();
				quest.text = this.quizXML..quest[index].text;
				quest.answerArray = new Array();
				for (var indexAnswer:String in this.quizXML..quest[index]..answer) {
					answer = new AnswerVO();
					answer.isCorrect = (this.quizXML..quest[index]..answer[indexAnswer].@isCorrect == "true")? true: false;
					answer.text = this.quizXML..quest[index]..answer[indexAnswer];
					quest.answerArray.push(answer);
					answer = null;
				}
				question = new Question(quest);
				this.questions.push(question);
				quest = null;
				question = null;
			}
			//MonsterDebugger.trace(this, this.questions);
		}
		
		
		protected function onBeginClick(event:MouseEvent):void {
			this.quizStage.begin.visible = false;
			this.quizStage.ok.visible = true;
			this.quizStage.title.visible = false;
			this.startQuiz();
		}
		
		private function startQuiz():void {
			
			if (this.quizCount < this.questions.length) {
				var quest:Question = this.questions[this.quizCount];
				
				this.quizStage.question.htmlText = quest.vo.text;
				quest.buildQuest(this.quizStage);
				//MonsterDebugger.trace(this, "Aew");
				this.quizStage.ok.addEventListener(MouseEvent.CLICK, onClickOK);
				this.quizStage.ok.buttonMode = true;
			}else {
				var eventComplete:FeedBackEvent = new FeedBackEvent(FeedBackEvent.COMPLETE, "Congratulations!", "You've done the quiz.", true);
				
				this.quizStage.dispatchEvent(eventComplete);
				
				this.quizStage.ok.visible = false;
				MonsterDebugger.trace("Complete", eventComplete);
			}
		}
		
		protected function onClickOK(event:MouseEvent):void {
			//MonsterDebugger.trace(this, "Aew 2");
			var quest:Question = this.questions[this.quizCount];
			//MonsterDebugger.trace(this, quest);
			var answerVO:AnswerVO = this.checkAnswers();
			//MonsterDebugger.trace("Aew 45", answerVO );
			if (answerVO) {
				if (answerVO.isCorrect) {
					var eventCorrect:FeedBackEvent = new FeedBackEvent(FeedBackEvent.TIP, "Congratulations!", "You're Right.", true);
					eventCorrect.time = this.time;
					
					this.quizStage.dispatchEvent(eventCorrect);
					MonsterDebugger.trace("Acertou", eventCorrect);
					
					quest.removeQuestion();
					this.quizCount++;
					this.startQuiz();
					
					this.time = 0;
					
				} else {
					//MonsterDebugger.trace(this, "Errou 1");
					var eventWrong:FeedBackEvent = new FeedBackEvent(FeedBackEvent.TIP, "Atention!", "Try again.", true);
					eventWrong.time = this.time;
					//MonsterDebugger.trace(this, "Errou 12");
					this.quizStage.dispatchEvent(eventWrong);
					MonsterDebugger.trace("Errou", eventWrong);
					//MonsterDebugger.trace(this, "Errou 2");
					quest.unselectAll();
					this.time = 0;
					
				}
			}else {
				var eventTip:FeedBackEvent = new FeedBackEvent(FeedBackEvent.TIP, "Atention!", "Select one answer.", true);
				this.quizStage.dispatchEvent(eventTip);
				MonsterDebugger.trace("Preencha!", eventTip);
			}
			
		}
		
		/**
		 * Função que checa se as respostas estão desmarcadas e retorna a marcada.
		 * @return AnswerVO
		 * @author caiocesarnevesdeoliveira
		 **/
		
		public function checkAnswers():AnswerVO {
			var retorno:AnswerVO = null;
			var quest:Question = this.questions[this.quizCount];
			//MonsterDebugger.trace(this, quest.vo.answerArray.length);
			try{
				for (var i:int = 0; i < quest.answers.length; i++) {
					
					if (Answer(quest.answers[i]).selected) {
						retorno = Answer(quest.answers[i]).vo;
						//break;
					}
				}
			} catch (e:Error) {
				//MonsterDebugger.trace("ERROR: ", e);
			}
			//MonsterDebugger.trace(this, retorno);
			return retorno;
		}
		
		protected function onTimerTick(event:Event):void
		{
			this.time += 1;
		}
	}
}